| Conditions | 10 |
| Total Lines | 107 |
| Code Lines | 89 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like HistoryMap.tsx ➔ HistoryMap often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | import { useState, useEffect } from 'react'; |
||
| 7 | |||
| 8 | |||
| 9 | export default function HistoryMap({navigation, journey, modalVisible, setModalVisible}): any { |
||
| 10 | const [startCoordinates, setStartCoordinates] = useState([]); |
||
| 11 | const [endCoordinates, setEndCoordinates] = useState([]); |
||
| 12 | const [journeyData, setJourneyData] = useState([]); |
||
| 13 | const [startStop, setStartStop] = useState([]); |
||
| 14 | |||
| 15 | useEffect(() => { |
||
| 16 | function setCoordinates() { |
||
| 17 | if (modalVisible) { |
||
| 18 | setStartCoordinates(journey['startPosition']); |
||
| 19 | setEndCoordinates(journey['endPosition']); |
||
| 20 | setJourneyData(journey); |
||
| 21 | } |
||
| 22 | |||
| 23 | }; |
||
| 24 | |||
| 25 | setCoordinates(); |
||
| 26 | }) |
||
| 27 | |||
| 28 | |||
| 29 | function getDistance() { |
||
| 30 | const travelDistance = mapModel.calcDistance(startCoordinates['latitude'], endCoordinates['longitude'], endCoordinates['latitude'], endCoordinates['longitude']); |
||
| 31 | |||
| 32 | return travelDistance.toFixed(2).toString(); |
||
| 33 | } |
||
| 34 | |||
| 35 | return ( |
||
| 36 | <Modal |
||
| 37 | animationType="slide" |
||
| 38 | transparent={true} |
||
| 39 | visible={modalVisible} |
||
| 40 | onRequestClose={() => { |
||
| 41 | setModalVisible(!modalVisible) |
||
| 42 | }} |
||
| 43 | > |
||
| 44 | <View style={styles.container}> |
||
| 45 | |||
| 46 | <MapView |
||
| 47 | style={styles.map} |
||
| 48 | region={{ |
||
| 49 | latitude: startCoordinates['latitude']? startCoordinates['latitude'] : 0, |
||
| 50 | longitude: startCoordinates['longitude']? startCoordinates['longitude'] : 0, |
||
| 51 | latitudeDelta: 0.03, |
||
| 52 | longitudeDelta: 0.03, |
||
| 53 | }} |
||
| 54 | userInterfaceStyle={'dark'} |
||
| 55 | > |
||
| 56 | |||
| 57 | |||
| 58 | <Marker |
||
| 59 | title='Start' |
||
| 60 | coordinate={{ |
||
| 61 | latitude: startCoordinates['latitude']? startCoordinates['latitude'] : 0, |
||
| 62 | longitude: startCoordinates['longitude']? startCoordinates['longitude'] : 0 |
||
| 63 | }} |
||
| 64 | /> |
||
| 65 | |||
| 66 | |||
| 67 | <Marker |
||
| 68 | pinColor='blue' |
||
| 69 | title='Stop' |
||
| 70 | coordinate={{ |
||
| 71 | latitude: endCoordinates['latitude']? endCoordinates['latitude'] : 0, |
||
| 72 | longitude: endCoordinates['longitude']? endCoordinates['longitude'] : 0 |
||
| 73 | }} |
||
| 74 | /> |
||
| 75 | |||
| 76 | </MapView> |
||
| 77 | |||
| 78 | <View style={styles.infoContainer}> |
||
| 79 | <Pressable style={[styles.backButton, styles.shadowProp]} onPress={() => setModalVisible(false)}> |
||
| 80 | <Icon |
||
| 81 | name='arrow-left' |
||
| 82 | size={25} |
||
| 83 | color='black' |
||
| 84 | /> |
||
| 85 | </Pressable> |
||
| 86 | <View style={styles.info}> |
||
| 87 | |||
| 88 | <View style={styles.listInfo}> |
||
| 89 | <Text style={styles.infoTitle}>Journey started</Text> |
||
| 90 | <Text>{journeyData['date']}</Text> |
||
| 91 | </View> |
||
| 92 | |||
| 93 | <View style={styles.listInfo}> |
||
| 94 | <Text style={styles.infoTitle}>Information about the trip</Text> |
||
| 95 | <Text>{getDistance()} km / {journeyData['totalMin']} min </Text> |
||
| 96 | |||
| 97 | <Text> {journeyData['totalPrice']} kr</Text> |
||
| 98 | </View> |
||
| 99 | |||
| 100 | <View style={styles.listInfo}> |
||
| 101 | <Text style={styles.infoTitle}>ID</Text> |
||
| 102 | <Text>{journeyData['_id']}</Text> |
||
| 103 | </View> |
||
| 104 | |||
| 105 | <View style={styles.listInfo}> |
||
| 106 | <Text style={styles.infoTitle}>Vehicle</Text> |
||
| 107 | <Text>{journeyData['scooterName']}</Text> |
||
| 108 | </View> |
||
| 109 | |||
| 110 | </View> |
||
| 111 | </View> |
||
| 112 | </View> |
||
| 113 | </Modal> |
||
| 114 | |||
| 187 |